package com.cqjtu.education.controller;

import com.cqjtu.education.entity.StudentCourse;
import com.cqjtu.education.service.StudentCourseService;
import com.cqjtu.education.utils.Result;
import com.github.pagehelper.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (StudentCourse)表控制层
 *
 * @author makejava
 * @since 2022-04-20 15:49:42
 */
@RestController
@Api(tags = "学生选课相关接口")
@RequestMapping("studentCourse")
public class StudentCourseController {
    /**
     * 服务对象
     */
    @Resource
    private StudentCourseService studentCourseService;


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @RequestMapping("/queryById")
    @ApiOperation(value = "queryById", notes = "通过id查询选课记录")
    public Result queryById(@RequestParam("id") String id) {
        try {
            return Result.success(this.studentCourseService.queryById(id));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("查询失败");
    }

    /**
     * 通过学生id查询选课信息
     *
     * @param id sid
     * @return 课程信息
     */
    @RequestMapping("/queryByStudentId")
    @ApiOperation(value = "queryByStudentId", notes = "通过学生id查询选课信息")
    public Result queryByStudentId(@RequestParam("id") String id) {
        try {
            return Result.success(this.studentCourseService.queryByStudentId(id));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("查询失败");
    }

    /**
     * 通过课程id查询学生信息
     *
     * @param id cid
     * @return 学生信息
     */
    @RequestMapping("/queryByCourseId")
    @ApiOperation(value = "queryByCourseId", notes = "通过课程id查询学生信息")
    public Result queryByCourseId(@RequestParam("id") String id) {
        try {
            return Result.success(this.studentCourseService.queryByCourseId(id));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("查询失败");
    }

    /**
     * @param cid
     * @param telList
     * @return
     */
    @RequestMapping("/addByTeacher")
    @ApiOperation(value = "addByTeacher", notes = "新增选课记录，通过教师传入课程id和学生手机号集合")
    public Result addByTeacher(@RequestParam("cid") String cid, @RequestParam("telList") List<String> telList) {
        try {
            return Result.success(studentCourseService.addByTeacher(cid, telList));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("新增失败");
    }

    /**
     * 新增数据
     *
     * @param studentCourse 实体
     * @return 新增结果
     */
    @RequestMapping("/add")
    @ApiOperation(value = "add", notes = "新增选课记录，即学生选课")
    public Result add(StudentCourse studentCourse) {
        try {
            if (this.studentCourseService.insert(studentCourse) == null) {
                return Result.fail("已添加该课程，无法重复添加课程");
            } else {
                return Result.success(studentCourse);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("新增失败");
    }

    /**
     * 编辑数据
     *
     * @param studentCourse 实体
     * @return 编辑结果
     */
    @RequestMapping("/edit")
    @ApiOperation(value = "edit", notes = "修改选课记录")
    public Result edit(StudentCourse studentCourse) {
        try {
            return Result.success(this.studentCourseService.update(studentCourse));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("修改失败");
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @RequestMapping("/deleteById")
    @ApiOperation(value = "deleteById", notes = "通过选课记录id删除选课记录")
    public Result deleteById(String id) {
        try {
            return Result.success(this.studentCourseService.deleteById(id));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("删除失败");
    }


    @RequestMapping("/deleteRecord")
    @ApiOperation(value = "deleteRecord", notes = "通过学生id和课程id删除选课记录")
    public Result deleteRecord(String cid, String sid) {
        try {
            return Result.success(this.studentCourseService.deleteRecord(cid, sid));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("删除失败");
    }

}

